package cn.fintecher.pangolin.service.dataimp.web;

import cn.fintecher.pangolin.common.enums.ImportTemplateType;
import cn.fintecher.pangolin.common.web.BaseController;
import cn.fintecher.pangolin.entity.elastic.ImportTemplate;
import cn.fintecher.pangolin.service.dataimp.model.request.CheckTemplateRequest;
import cn.fintecher.pangolin.service.dataimp.model.response.CheckTemplateResponse;
import cn.fintecher.pangolin.service.dataimp.request.AddImportTemplateRequest;
import cn.fintecher.pangolin.service.dataimp.request.AnalyzeExcelRequest;
import cn.fintecher.pangolin.service.dataimp.request.FindImportTemplateRequest;
import cn.fintecher.pangolin.service.dataimp.request.ModifyTemplateRequest;
import cn.fintecher.pangolin.service.dataimp.response.AnalyzeExcelResponse;
import cn.fintecher.pangolin.service.dataimp.response.FindImportTemplateResponse;
import cn.fintecher.pangolin.common.model.PropertyResponse;
import cn.fintecher.pangolin.service.dataimp.response.TemplateResponse;
import cn.fintecher.pangolin.service.dataimp.service.TemplateCommonService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Auther: xiaqun
 * @Description: 案件模板相关接口
 * @Date: 10:24 2019/8/24
 */

@RestController
@RequestMapping("/api/importTemplateController")
@Api(value = "案件模板相关接口", description = "案件模板相关接口")
public class ImportTemplateController extends BaseController {
    @Autowired
    private TemplateCommonService templateCommonService;

    /**
     * 多条件查询案件模板
     */
    @ApiOperation(value = "多条件查询案件模板", notes = "多条件查询案件模板")
    @GetMapping("/findAllImportTemplate")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query",
                    value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query",
                    value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",
                    value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<FindImportTemplateResponse>> findAllImportTemplate(FindImportTemplateRequest request,
                                                                                  Pageable pageable,
                                                                                  @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to find all import template,{}", request);
        Page<FindImportTemplateResponse> page = templateCommonService.findAllImportTemplate(request, pageable, token);
        return ResponseEntity.ok().body(page);
    }

    /**
     * 解析excel表头
     */
    @ApiOperation(value = "解析excel表头", notes = "解析excel表头")
    @PostMapping("/analyzeExcelTitle")
    public ResponseEntity<List<AnalyzeExcelResponse>> analyzeExcelTitle(@Valid @RequestBody AnalyzeExcelRequest request) throws Exception {
        log.info("REST request to analyze excel title,{}", request);
        List<AnalyzeExcelResponse> list = templateCommonService.analyzeExcelTitle(request);
        return ResponseEntity.ok().body(list);
    }

    /**
     * 获取案件字段
     */
    @ApiOperation(value = "获取案件字段", notes = "获取案件字段")
    @GetMapping("/getCaseProperty")
    public ResponseEntity<List<PropertyResponse>> getCaseProperty(@RequestParam @ApiParam(value = "模板类型", required = true) ImportTemplateType type) {
        log.info("REST request to get case property,{}", type);
        List<PropertyResponse> list = templateCommonService.getCaseProperty(type);
        return ResponseEntity.ok().body(list);
    }

    /**
     * 新增模板
     */
    @ApiOperation(value = "新增模板", notes = "新增模板")
    @PostMapping("/addImportTemplate")
    public ResponseEntity<Void> addImportTemplate(@Valid @RequestBody AddImportTemplateRequest request,
                                                  @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to add import template,{}", request);
        templateCommonService.addImportTemplate(request, token);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除模板
     */
    @ApiOperation(value = "删除模板", notes = "删除模板")
    @DeleteMapping("/deleteTemplate")
    public ResponseEntity<Void> deleteTemplate(@RequestParam @ApiParam(value = "模板ID", required = true) String templateId) {
        log.info("REST request to delete template,{}", templateId);
        templateCommonService.deleteTemplate(templateId);
        return ResponseEntity.ok().build();
    }

    /**
     * 获取模板信息
     */
    @ApiOperation(value = "获取模板信息", notes = "获取模板信息")
    @GetMapping("/getImportTemplateById")
    public ResponseEntity<ImportTemplate> getImportTemplateById(@RequestParam @ApiParam(value = "模板ID", required = true) String templateId) {
        log.info("REST request to get import template by id,{}", templateId);
        ImportTemplate importTemplate = templateCommonService.getImportTemplateById(templateId);
        return ResponseEntity.ok().body(importTemplate);
    }

    /**
     * 修改模板
     */
    @ApiOperation(value = "修改模板", notes = "修改模板")
    @PutMapping("/modifyImportTemplate")
    public ResponseEntity<Void> modifyImportTemplate(@Valid @RequestBody ModifyTemplateRequest request,
                                                     @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to modify template,{}", request);
        templateCommonService.modifyImportTemplate(request, token);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询委托方下模板
     */
    @ApiOperation(value = "查询委托方下模板", notes = "查询委托方下模板")
    @GetMapping("/getTemplateByPrincipal")
    public ResponseEntity<List<TemplateResponse>> getTemplateByPrincipal(@RequestParam @ApiParam(value = "委托方ID", required = true) String principalId,
                                                                         @RequestParam @ApiParam(value = "模板类型", required = true) ImportTemplateType type) {
        log.info("REST request to get template by principal,{},{}", principalId, type);
        List<TemplateResponse> list = templateCommonService.getTemplateByPrincipal(principalId, type);
        return ResponseEntity.ok().body(list);
    }

    /**
     * 解析Excel数据模板头信息
     */
    @ApiOperation(value = "解析Excel数据模板头信息", notes = "解析Excel数据模板头信息")
    @PostMapping(value = "/checkTemplate")
    public ResponseEntity<CheckTemplateResponse> checkTemplate(@RequestBody CheckTemplateRequest request) throws Exception {
        log.info("REST request to check template,{}", request);
        CheckTemplateResponse response = templateCommonService.checkTemplate(request);
        return ResponseEntity.ok().body(response);
    }
}